package com.tzjw.marketweixin.util;


import com.tzjw.marketweixin.mapper.MarketSystemMapper;
import com.tzjw.marketweixin.pojo.bean.MarketSystem;
import com.tzjw.marketweixin.pojo.bean.MarketSystemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * market_system全表配置
 *
 * @author hayden
 * @since 2023/09/14 23:12
 */
@Component
public class GlobalConfig {
    private static Map<String,String> marketSystemConfig;

    static MarketSystemMapper systemMapper;

    //查询条件
    static MarketSystemExample systemExample;

    @Autowired
    public void setSystemMapper(MarketSystemMapper systemMapper) {
        GlobalConfig.systemMapper = systemMapper;
    }

    static {
        //初始化配置
        marketSystemConfig = new HashMap<>();
        //初始化查询条件
        systemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andDeletedEqualTo(false);
    }

    /**
     * 根据key获取配置
     * @param keyName key
     * @return 配置值String 注意转换类型
     */
    public static String getMarketSystemConfig(String keyName){
        updateConfig();
        return marketSystemConfig.get(keyName);
    }

    /**
     * 更新marketSystemConfig配置
     */
    private static void updateConfig() {
        List<MarketSystem> marketSystems = systemMapper.selectByExample(systemExample);
        for (MarketSystem config : marketSystems) {
            marketSystemConfig.put(config.getKeyName(), config.getKeyValue());
        }
    }

}


